package com.zxl.manager.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * Created by jimmy on 2017/7/13.
 */
public class ImageUtil {

    //设置好账号的ACCESS_KEY和SECRET_KEY
    private final static String ACCESS_KEY = "KlXGGDOZY3Zn948ma2zUTvXWjQE9ARGFi-4zwz6l"; //这两个登录七牛 账号里面可以找到
    private final static String SECRET_KEY = "psYM4olvoZ9n5ACUcv0vuhvioaVY5A9hDvq3F7NC";
    private final static String bucketname = "jimmyzhang";

    private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    //创建上传对象
    private static UploadManager uploadManager = new UploadManager(new Configuration());


    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public static String getUpToken(){
        return auth.uploadToken(bucketname);
    }

    public static void upload(String key,String path){
        try {

            //调用put方法上传
            Response res = uploadManager.put(path+"/"+key, key, getUpToken());
            //打印返回的信息
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        }
    }
}
